package com.roncoo.education.oauth.config;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;

@Service
public class ClientDetailsServiceImpl implements ClientDetailsService {

	public final static int accessTokenValiditySeconds = 60 * 60 * 24;

	@Override
	public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
		// TODO Auto-generated method stub
		return new ClientDetails() {
			
			@Override
			public boolean isSecretRequired() {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public boolean isScoped() {
				// TODO Auto-generated method stub
				return true;
			}
			
			@Override
			public boolean isAutoApprove(String scope) {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public Set<String> getScope() {
				Set set = new HashSet<String>();
				set.add("callback:score");
				return set;
			}
			
			@Override
			public Set<String> getResourceIds() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public Set<String> getRegisteredRedirectUri() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public Integer getRefreshTokenValiditySeconds() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public String getClientSecret() {
				// TODO Auto-generated method stub
				return new BCryptPasswordEncoder().encode("12345697d");
			}
			
			@Override
			public String getClientId() {
				return clientId;
			}
			
			@Override
			public Set<String> getAuthorizedGrantTypes() {
				Set set = new HashSet<String>();
				set.add("authorization_code");
				set.add("refresh_token");
				set.add("implicit");
				set.add("password");
				set.add("client_credentials");
				return set;
			}
			
			@Override
			public Collection<GrantedAuthority> getAuthorities() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public Map<String, Object> getAdditionalInformation() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public Integer getAccessTokenValiditySeconds() {
				// 2 小时过期
				return accessTokenValiditySeconds;
			}
		};
	}

}
